#  Copyright (c) 2010
#  The Regents of the University of Michigan
#  All Rights Reserved

#  Permission is granted to use, copy, create derivative works, and
#  redistribute this software and such derivative works for any purpose,
#  so long as the name of the University of Michigan is not used in
#  any advertising or publicity pertaining to the use or distribution
#  of this software without specific, written prior authorization. If
#  the above copyright notice or any other identification of the
#  University of Michigan is included in any copy of any portion of
#  this software, then the disclaimer below must also be included.

#  This software is provided as is, without representation or warranty
#  of any kind either express or implied, including without limitation
#  the implied warranties of merchantability, fitness for a particular
#  purpose, or noninfringement.  The Regents of the University of
#  Michigan shall not be liable for any damages, including special,
#  indirect, incidental, or consequential damages, with respect to any
#  claim arising out of or in connection with the use of the software,
#  even if it has been or is hereafter advised of the possibility of
#  such damages.
import matplotlib
matplotlib.use('cairo.png')
from matplotlib import pyplot

from util import *

import cPickle as pickle
import time
funcs = []
plotters = {}

def todo(filename):
  def dec(f):
    f.filename = filename
    funcs.append(f)
    return f
  return dec

def plot_by(plotter):
  def dec(f):
    plotters[f] = plotter
    return f
  return dec

def get_ip_to_ports_count_map():
  conn = get_db_conn()
  c = conn.cursor()
  c.execute('SET work_mem=\'3GB\';')
  c.execute("""SELECT P.peer, COUNT(DISTINCT P.port) AS cnt
               FROM peer_lists_god_interesting P
               GROUP BY P.peer
               ORDER BY cnt ASC;""")
  return c.fetchall()
  

#@todo('ip_to_content.dat')
def get_ip_to_content_map():
  conn = get_db_conn()
  c = conn.cursor()
  c.execute('SET work_mem=\'3GB\';')
  c.execute("""SELECT P.peer, COUNT(DISTINCT P.dhtkey) AS cnt
               FROM peer_lists_god_interesting P
               GROUP BY P.peer
               ORDER BY cnt ASC;""")
  return c.fetchall()


#@todo('ip_to_all_content.dat')
def get_ip_to_all_content_map():
  conn = get_db_conn()
  c = conn.cursor()
  c.execute('SET work_mem=\'3GB\';')
  c.execute("""SELECT P.peer, COUNT(DISTINCT P.dhtkey) AS cnt
               FROM peer_lists_god P
               GROUP BY P.peer
               ORDER BY cnt ASC;""")
  return c.fetchall()


@todo('content_to_ip.dat')
def get_content_to_ip_map():
  conn = get_db_conn()
  c = conn.cursor()
  c.execute('SET work_mem=\'3GB\';')
  c.execute("""SELECT P.dhtkey, COUNT(DISTINCT P.peer) AS cnt
               FROM peer_lists_god_interesting P
               GROUP BY P.dhtkey
               ORDER BY cnt ASC;""")
  return [(str(x), int(y)) for (x,y) in c.fetchall()]

# @todo('ip_port_to_content.dat')
# def get_ip_port_to_content_map():
#   conn = get_db_conn()
#   c = conn.cursor()
#   c.execute('SET work_mem=\'1GB\';')
#   c.execute("""SELECT P.peer, P.port, COUNT(DISTINCT P.dhtkey) AS cnt
#                FROM peer_lists_god P
#                GROUP BY P.peer, P.port
#                ORDER BY cnt ASC;""")
#   return c.fetchall()

# @todo('ip_port_to_identifiable_content.dat')
# def get_ip_port_to_identifiable_content_map():
#   conn = get_db_conn()
#   c = conn.cursor()
#   c.execute('SET work_mem=\'1GB\';')
#   c.execute("""SELECT P.peer, P.port, COUNT(DISTINCT D.hash) AS cnt
#                FROM peer_lists_god P, torrent_descs_god D
#                WHERE digest(D.hash, 'sha1') = P.dhtkey
#                GROUP BY P.peer, P.port
#                ORDER BY cnt ASC;""")
#   return c.fetchall()


#def dump_all_data():

def dump_all_data():
  print time.asctime()
  for func in funcs:
    res = func()
    pickle.dump(res, open(func.filename, 'w'), pickle.HIGHEST_PROTOCOL)
    print 'Don\'t forget to remove the todo for function %s!' % func.func_name
    print time.asctime()
    del res

if __name__ == '__main__':
  dump_all_data()
